package tech.yunjing.train.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.yunjing.train.dao.RedPacketDao;
import tech.yunjing.train.entity.RedPacket;
import tech.yunjing.train.service.RedPacketService;
import tech.yunjing.train.service.exception.RedPacketAmountException;
import tech.yunjing.train.service.exception.RedPacketNameException;
import tech.yunjing.train.service.exception.SmallRedPacketNumberException;
import tech.yunjing.train.service.exception.UserLoginException;
import tech.yunjing.train.util.Utility;

import java.util.Date;
import java.util.List;

/**
 * 实现RedPacketService中的方法
 * Created by 刘飞 on 2017-6-8.
 */
@Service
@Transactional
public class RedPacketServiceImp implements RedPacketService {
    @Autowired
    private RedPacketDao redPacketDao;
    @Autowired
    private SmallRedPacketServiceimp smallRedPacketServiceimp;

    /**
     * 红包增加的方法  首先判断其参数为不为空，为空则抛出异常
     * 其次根据名字和状态来判断该红包在数据库中存不存在
     *存在则抛出异常。最后执行增加的方法
     * @param redPacketName
     * @param redPacketAmount
     * @param smallPacketNumber
     * @return
     * @throws RedPacketAmountException
     * @throws SmallRedPacketNumberException
     * @throws RedPacketNameException
     */
    @Override
    public int addRedPacket(String redPacketName, Double redPacketAmount, Integer smallPacketNumber,String userAccount)throws RedPacketAmountException,SmallRedPacketNumberException,RedPacketNameException {

        RedPacket redPacket=redPacketDao.findByRedPacketNameAndLogicDeleteAndUserAccount(redPacketName,"0",userAccount);
        if(redPacket!=null){
            throw new RedPacketNameException("红包已存在,请修改名字");
        }
        return redPacketDao.addRedpacket(redPacketName,smallPacketNumber,redPacketAmount,new Date(),null, Utility.getStopDate(),userAccount,0);
    }

    /**
     * 红包逻辑删除方法
     * 首先判断其红包名为不为空，其次判断其在数据库中存不存在
     * 抛出对应异常 修改起状态
     * @param redPacketName
     * @return
     * @throws RedPacketNameException
     */

    @Override
    public int removePacket(String redPacketName,String userAccount)throws RedPacketNameException {

        RedPacket redPacket=redPacketDao.findByRedPacketNameAndLogicDeleteAndUserAccount(redPacketName,"0",userAccount);
        if(redPacket==null){
            throw new RedPacketNameException("红包已经被删除,请确认红包名称是否正确");
        }
        return redPacketDao.removeRedPacket("1",new Date(),redPacketName,userAccount,"0",new Date());
    }

    /**
     * 红包修改的方法，根据其名字状态在数据库中查找到红包 修改小红包数量和金额
     * 若字符或者红包不存在 抛出对应异常
     * @param redPacketName
     * @param smallRedPacketNumber
     * @param redPacketAmount
     * @return
     * @throws RedPacketAmountException
     * @throws SmallRedPacketNumberException
     * @throws RedPacketNameException
     */
    @Override
    public int updateRedPacket(String redPacketName, Integer smallRedPacketNumber, Double redPacketAmount,String userAccount) throws RedPacketAmountException, SmallRedPacketNumberException, RedPacketNameException {
        RedPacket redPacket=redPacketDao.findByRedPacketNameAndLogicDeleteAndUserAccount(redPacketName,"0",userAccount);
        if(redPacket==null){
            throw new RedPacketNameException("红包不存在修改失败");
        }
        return redPacketDao.updateNumberAndAmount(smallRedPacketNumber,redPacketAmount,new Date(), Utility.getStopDate(),redPacketName,userAccount,"0");
    }

    /**
     * 红包根据状态查找方法，本项目暂时未用到
     * 提供给后期维护数据库时使用
     * @return
     */
    @Override
    public List<RedPacket> findAllByLogicDelete() {
        return redPacketDao.findAllByLogicDelete("1");
    }

    /**
     * 根据红包名称查询红包的方法
     * 要满足其在数据库中名字存在而且状态不为2 才可以查到
     * 否则抛出对应异常。
     * @param redPacketName
     * @return
     * @throws RedPacketNameException
     */
    @Override
    public RedPacket findRedPacketByRedPacketName(String redPacketName, String userAccount)throws RedPacketNameException {
        if(redPacketName==null||redPacketName.trim().isEmpty()){
            throw new RedPacketNameException("红包名称不能为空");
        }
        if(userAccount==null||userAccount.trim().isEmpty()){
            throw new UserLoginException("红包账户不能为空");
        }
        RedPacket redPacket=redPacketDao.findByRedPacketNameAndLogicDeleteAndUserAccount(redPacketName,"0",userAccount);
        if(redPacket==null){
            throw new RedPacketNameException("红包不存在请确认红包名称是否正确");
        }
        return redPacket;
    }
}